package com.huang.controller;

import com.huang.common.utils.SecurityAuthenticationUtils;
import com.huang.domain.SysMenu;
import com.huang.service.SysMenuService;
import com.huang.vo.MenuAndAuthenticationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hqy
 * @date 2022年06月21日 16:35
 */
@RestController
@RequestMapping("/sys/menu")
@Api(tags = "系统菜单获取接口文档")
public class MenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("/nav")
    @ApiOperation("获取系统的菜单以及用户的权限集合")
    public ResponseEntity<MenuAndAuthenticationVo> loadMenuAndAuthentication(){
        MenuAndAuthenticationVo menuAndAuthenticationVo = new MenuAndAuthenticationVo();
        //通过上下文对象获取用户的权限信息
        List<String> authorities = SecurityAuthenticationUtils.getAuthentication().getAuthorities()
                .stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
        menuAndAuthenticationVo.setAuthorities(authorities);

        List<SysMenu> menus = sysMenuService.loadSysUserMenus(SecurityAuthenticationUtils.getUserId());
        menuAndAuthenticationVo.setMenuList(menus);

        return ResponseEntity.ok(menuAndAuthenticationVo);

    }
}
